<template>
  <div id="registerView">
    <div class="box">
      <div class="header">
        <div class="login" @click="goToLogin">登录</div>
        <div class="register">注册</div>
      </div>
      <div class="form">
        <a-form
          ref="formRef"
          class="formInfo"
          :model="form"
          :rules="rules"
          label-align="left"
          auto-label-width="true"
          @submit-success="handleSubmit"
        >
          <a-form-item field="userAccount" label="账号">
            <a-input
              v-model="form.userAccount"
              placeholder="请输入账号（至少4位）..."
            />
          </a-form-item>
          <a-form-item field="userPassword" label="密码">
            <a-input
              v-model="form.userPassword"
              type="password"
              placeholder="请输入密码（至少8位）..."
            />
          </a-form-item>
          <a-form-item field="checkPassword" label="确认密码">
            <a-input
              v-model="form.checkPassword"
              type="password"
              placeholder="请再次输入密码..."
            />
          </a-form-item>
          <a-form-item>
            <a-button html-type="submit" style="width: 100%">注册</a-button>
          </a-form-item>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, watch } from "vue";
import { useRouter } from "vue-router";
import { UserControllerService } from "../../../generated";
const router = useRouter();
// 表单数据
const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
});

// 表单验证规则
const rules = {
  userAccount: [
    {
      required: true,
      message: "请输入账号",
      trigger: "blur",
    },
    {
      minLength: 4,
      message: "账号长度不能少于4位",
      trigger: "blur",
    },
    { maxLength: 20, message: "账号长度不能超过12位", trigger: "blur" },
    {
      match: /^[a-zA-Z0-9_]+$/,
      message: "账号仅支持字母、数字和下划线",
      trigger: "blur",
    },
  ],
  userPassword: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
    { minLength: 8, message: "密码长度不能少于8位", trigger: "blur" },
    { maxLength: 20, message: "密码长度不能超过20位", trigger: "blur" },
    {
      match: /^(?=.*[a-zA-Z])(?=.*\d).+$/,
      message: "密码需包含至少1个字母和1个数字",
      trigger: "blur",
    },
  ],
  checkPassword: [
    {
      required: true,
      message: "请确认密码",
      trigger: "blur",
    },
    {
      validator: (value: string, cb: any) => {
        if (value !== form.userPassword) {
          cb("两次密码不一样");
        } else {
          cb();
        }
      },
    },
  ],
};

// 注册处理
const handleSubmit = async () => {
  // 验证通过，调用注册接口（示例）
  console.log("注册数据：", form);
  // 实际项目中替换为真实接口调用
  const res = await UserControllerService.userRegisterUsingPost(form);
  if (res.code === 0) {
    alert("注册成功，即将跳转到登录页");
    router.push("/user/login");
  } else {
    alert(res.message);
  }
};

// 切换到登录页面
const goToLogin = () => {
  router.push("/user/login");
};
</script>

<style>
#registerView .box {
  background-color: #efefef;
  width: 350px; /* 适当加宽提升体验 */
  min-height: 300px;
  margin: 80px auto;
  border-radius: 8px;
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

#registerView .box .header {
  display: flex;
  background-color: #c0eeec;
  height: 50px;
}

#registerView .box .header .login {
  background-color: #6860ee;
  width: 100px;
  font-size: 15px;
  line-height: 50px;
  text-align: center;
  color: white;
}

#registerView .box .header .register {
  background-color: #b6ee55;
  flex: 1; /* 自适应剩余宽度 */
  font-size: 30px;
  line-height: 50px;
  text-align: center;
}

#registerView .box .header .login:hover,
#registerView .box .header .register:hover {
  cursor: pointer;
  opacity: 0.9;
}

#registerView .box .form {
  width: 100%;
  padding: 20px;
  box-sizing: border-box; /* 避免宽度溢出 */
}

/* 错误提示样式 */
.ant-form-item-explain-error {
  color: #ff4d4f;
  font-size: 12px;
  margin-top: 4px;
}

/* 输入框聚焦样式优化 */
.ant-input:focus {
  border-color: #6860ee;
  box-shadow: 0 0 0 2px rgba(104, 96, 238, 0.2);
}
</style>
